package com.yoyo.ad.yoyo;

import android.content.Context;
import android.graphics.Color;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;

import com.bumptech.glide.Glide;
import com.yoyo.ad.R;
import com.yoyo.ad.bean.YoYoAdBean;
import com.yoyo.ad.confusion.AdManage;
import com.yoyo.ad.main.IAdBannerListener;
import com.yoyo.ad.main.IAdClick;
import com.yoyo.ad.main.IAdFactory_;
import com.yoyo.ad.main.IAdInteractionListener;
import com.yoyo.ad.main.IAdView;
import com.yoyo.ad.main.YoYoAd;
import com.yoyo.ad.presenter.AdPresenter;
import com.yoyo.ad.utils.AnalysisUtils;
import com.yoyo.ad.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;


public class YYAdFactory implements IAdFactory_ {

    private IAdView mAdView;
    private IAdBannerListener mAdBannerListener;
    private IAdInteractionListener mAdInteractionListener;
    private Context mContext;

    private SparseArray<Long> adIds = new SparseArray<>();
    private SparseBooleanArray isIpReports = new SparseBooleanArray();
    private final int adType = AdManage.YOYO;
    private Disposable mDisposable;
    private Disposable mBannerDisposable;
    private Disposable mInteractionDisposable;

    public YYAdFactory(Context context) {
        mContext = context;
    }

    @Override
    public void setAdView(IAdView adView) {
        if (mAdView == null && adView != null)
            mAdView = adView;
    }


    @Override
    public void setAdBannerListener(IAdBannerListener adView) {
        if (mAdBannerListener == null && adView != null)
            mAdBannerListener = adView;
    }

    @Override
    public void setAdInteractionListener(IAdInteractionListener adInteractionListener) {
        if (mAdInteractionListener == null && adInteractionListener != null)
            mAdInteractionListener = adInteractionListener;
    }

    @Override
    public void setConfig(long adId, int adPositionId, boolean isIpReport) {
        adIds.put(adPositionId, adId);
        isIpReports.put(adPositionId, isIpReport);
    }

    @Override
    public void getYoyoAd(final int position, final int requestCode, int size, String adPlaceId, int type) {
        mDisposable = new AdPresenter(mContext, null).qryYoyoAdConfig(adPlaceId, new IYoyoAdListener() {
            @Override
            public void success(YoYoAdBean yoYoAdBean) {
                AdManage.showAdSuccess(mContext, adType, adIds.get(position, 0L),
                        position, isIpReports.get(position, false));
                List<YoYoAd> list = new ArrayList<>(1);
                if (yoYoAdBean != null) {
                    YYAd e = new YYAd(yoYoAdBean, new IAdClick() {
                        @Override
                        public void onAdClick(View view) {
                            AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_CLICK);
                            if (mAdView != null)
                                mAdView.onAdClick(requestCode, view);
                        }

                        @Override
                        public void onAdShow() {
                            AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_PRESENT);
                        }
                    });
                    list.add(e);
                } else {
                    AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_FAIL);
                }
                if (mAdView != null)
                    mAdView.adSuccess(requestCode, list);
            }

            @Override
            public void fail(String error) {
                AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_FAIL);
                if (mAdView != null)
                    mAdView.adFail(requestCode, error);
            }
        });
    }

    @Override
    public void getExpressAd(final int position, final int requestCode, String adPlaceId, int size, int width) {
        mDisposable = new AdPresenter(mContext, null).qryYoyoAdConfig(adPlaceId, new IYoyoAdListener() {
            @Override
            public void success(YoYoAdBean yoYoAdBean) {
                AdManage.showAdSuccess(mContext, adType, adIds.get(position, 0L),
                        position, isIpReports.get(position, false));
                List<YoYoAd> list = new ArrayList<>(1);
                if (yoYoAdBean != null) {
                    YYAd e = new YYAd(yoYoAdBean, new IAdClick() {
                        @Override
                        public void onAdClick(View view) {
                            AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_CLICK);
                            if (mAdView != null)
                                mAdView.onAdClick(requestCode, view);
                        }

                        @Override
                        public void onAdShow() {
                            AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_PRESENT);
                        }
                    });
                    list.add(e);
                } else {
                    AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_FAIL);
                }
                if (mAdView != null)
                    mAdView.adSuccess(requestCode, list);
            }

            @Override
            public void fail(String error) {
                AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_FAIL);
                if (mAdView != null)
                    mAdView.adFail(requestCode, error);
            }
        });
    }

    @Override
    public void getBanner(final int position, final int requestCode, String adPlaceId, final ViewGroup adsParent, int width, int height) {
        mBannerDisposable = new AdPresenter(mContext, null).qryYoyoAdConfig(adPlaceId, new IYoyoAdListener() {
            @Override
            public void success(YoYoAdBean yoYoAdBean) {
                AdManage.showAdSuccess(mContext, adType, adIds.get(position, 0L),
                        position, isIpReports.get(position, false));
                if (yoYoAdBean != null) {
                    YYAd e = new YYAd(yoYoAdBean, new IAdClick() {
                        @Override
                        public void onAdClick(View view) {
                            AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_CLICK);
                            if (mAdBannerListener != null)
                                mAdBannerListener.adClick(requestCode);
                        }

                        @Override
                        public void onAdShow() {
                            AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_PRESENT);
                        }
                    });
                    adsParent.removeAllViews();
                    ImageView view = new ImageView(mContext);
                    Glide.with(view).load(e.getImgUrl1()).into(view);
                    adsParent.addView(view);
                    e.onAdClicked(view, adsParent);
                    if (mAdBannerListener != null)
                        mAdBannerListener.adShow(requestCode);
                } else {
                    AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_FAIL);
                    if (mAdView != null)
                        mAdView.adFail(requestCode, "no ad");
                }
            }

            @Override
            public void fail(String error) {
                AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_FAIL);
                if (mAdView != null)
                    mAdView.adFail(requestCode, error);
            }
        });
    }

    @Override
    public void getInteraction(final int position, final int requestCode, String adPlaceId) {
        mInteractionDisposable = new AdPresenter(mContext, null).qryYoyoAdConfig(adPlaceId, new IYoyoAdListener() {
            @Override
            public void success(YoYoAdBean yoYoAdBean) {
                AdManage.showAdSuccess(mContext, adType, adIds.get(position, 0L),
                        position, isIpReports.get(position, false));
                if (yoYoAdBean != null) {
                    YYAd e = new YYAd(yoYoAdBean, new IAdClick() {
                        @Override
                        public void onAdClick(View view) {
                            AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_CLICK);
                            if (mAdInteractionListener != null)
                                mAdInteractionListener.adClick(requestCode);
                        }

                        @Override
                        public void onAdShow() {
                            AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_PRESENT);
                            if (mAdInteractionListener != null)
                                mAdInteractionListener.adShow(requestCode);
                        }
                    });
                    final ConstraintLayout constraintLayout = new ConstraintLayout(mContext);
//                    constraintLayout.setBackgroundResource(R.color.tt_trans_half_black);
//                    ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(
//                            0,
//                            ConstraintSet.WRAP_CONTENT);
//                    layoutParams.matchConstraintPercentWidth = 0.7f;
                    constraintLayout.setBackgroundColor(Color.argb(77, 0, 0, 0));
                    constraintLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                    ImageView iv = new ImageView(mContext);
                    ImageView closeIv = new ImageView(mContext);
                    iv.setId(R.id.interaction_iv);
                    e.onAdClicked(iv, null);
                    closeIv.setId(R.id.cancel_btn);
                    View.OnClickListener clickListener = new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            constraintLayout.setVisibility(View.GONE);
                            if (mAdInteractionListener != null)
                                mAdInteractionListener.adDismissed(requestCode);
                        }
                    };
                    closeIv.setOnClickListener(clickListener);
                    constraintLayout.setOnClickListener(clickListener);
                    closeIv.setImageResource(R.mipmap.ic_close);
                    Glide.with(iv).load(e.getImgUrl1()).into(iv);
                    constraintLayout.addView(iv);
                    constraintLayout.addView(closeIv);
                    ConstraintSet constraintSet = new ConstraintSet();
                    constraintSet.connect(R.id.interaction_iv, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
                    constraintSet.connect(R.id.interaction_iv, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
                    constraintSet.connect(R.id.interaction_iv, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
                    constraintSet.connect(R.id.interaction_iv, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
                    constraintSet.connect(R.id.cancel_btn, ConstraintSet.TOP, R.id.interaction_iv, ConstraintSet.BOTTOM, DensityUtil.dp2px(mContext, 12));
                    constraintSet.connect(R.id.cancel_btn, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
                    constraintSet.connect(R.id.cancel_btn, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
                    constraintSet.constrainPercentWidth(R.id.interaction_iv, 0.7f);
                    constraintSet.constrainHeight(R.id.interaction_iv, ConstraintSet.WRAP_CONTENT);
                    constraintSet.constrainWidth(R.id.cancel_btn, DensityUtil.dp2px(mContext, 32));
                    constraintSet.constrainHeight(R.id.cancel_btn, ConstraintSet.WRAP_CONTENT);
                    constraintSet.applyTo(constraintLayout);
                    e.load(constraintLayout);
                    if (mAdInteractionListener != null)
                        mAdInteractionListener.adReady(requestCode, e);
                } else {
                    AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_FAIL);
                    if (mAdInteractionListener != null)
                        mAdInteractionListener.adFail(requestCode, "no ad");
                }
            }

            @Override
            public void fail(String error) {
                AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_FAIL);
                if (mAdInteractionListener != null)
                    mAdInteractionListener.adFail(requestCode, error);
            }
        });
    }

    @Override
    public void onDestroy() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
        if (mBannerDisposable != null && !mBannerDisposable.isDisposed()) {
            mBannerDisposable.dispose();
        }
        if (mInteractionDisposable != null && !mInteractionDisposable.isDisposed()) {
            mInteractionDisposable.dispose();
        }
    }
}
